package com.zc.controller;

import com.github.pagehelper.PageInfo;
import com.zc.dto.StuTestDto;
import com.zc.Enum.ErrorEnum;
import com.zc.annotation.SystemLogAnnotation;
import com.zc.pojo.StuTest;
import com.zc.service.StuTestService;
import com.zc.utils.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.UUID;

/**
 * 1.StuTestController测试类
 */

@RestController
@RequestMapping("stuTest")
@Slf4j
public class StuTestController {

    @Autowired
    private StuTestService stuTestService;

    @RequestMapping("creat")
    public JSONResult creat(@Valid @RequestBody StuTestDto testBO, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return JSONResult.errorMsg(ErrorEnum.C_10000, bindingResult);
        }

        String id = UUID.randomUUID().toString();
        StuTest stuTest = new StuTest();
        BeanUtils.copyProperties(testBO, stuTest);
        stuTest.setId(id);

        log.info("stuTest=" + stuTest);
        stuTestService.stuTestSave(stuTest);

        return JSONResult.ok();
    }


    @GetMapping("queryById")
    public JSONResult queryById(@RequestParam String id) {
        log.info("id=" + id);
        StuTest stuTest = stuTestService.queryById(id);

        return JSONResult.ok(stuTest);
    }

    @SystemLogAnnotation("分页查询测试")//主要看这里
    @GetMapping("queryByPage")
    public JSONResult queryByPage(@RequestParam Integer pageNum) {
        log.error("当前执行：{}", "StuTestController.queryByPage");
        PageInfo<StuTest> pageInfo = stuTestService.queryByPage(pageNum);
        return JSONResult.ok(pageInfo);
    }

}
